package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Resource;
import com.zrrd.yunchmall.user.entity.ResourceCategory;
import com.zrrd.yunchmall.user.entity.Role;
import com.zrrd.yunchmall.user.service.IResourceService;
import com.zrrd.yunchmall.user.service.IRoleService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/resource")
@Api("权限服务-资源模块")
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @ApiOperation("查询所有资源信息")
    @GetMapping("/listAll")
    public ResponseResult listAll() {
        return new ResponseResult(200, "成功查询到所有的资源", resourceService.list());
    }

    @ApiOperation("查询资源列表")
    @GetMapping("/list")
    public ResponseResult list(@ApiParam(required = true, defaultValue = "1") @RequestParam int pageSize,
                               @ApiParam(required = true, defaultValue = "10") @RequestParam int pageNum,
                               @ApiParam("资源关键字") String nameKeyword,
                               @ApiParam("资源url") String urlKeyword,
                               @ApiParam("资源分类Id") Integer categoryId){
        QueryWrapper<Resource> queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(nameKeyword)){//当关键字不为空
            //where username = ? or nick_name = keyword
            queryWrapper.like("name", nameKeyword);
        }
        if(urlKeyword != null){
            queryWrapper.eq("url" ,urlKeyword);
        }
        if(categoryId != null){
            queryWrapper.eq("category_id" ,categoryId);
        }
        return new ResponseResult(200, "查询成功",
                resourceService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }

    @ApiOperation("删除资源")
    @PostMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") long id) {
        resourceService.removeById(id);
        return new ResponseResult(200,"删除成功");
    }

    @ApiOperation("添加一个资源")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Resource resource){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", resource.getName());
        queryWrapper.eq("url",resource.getUrl());
        queryWrapper.eq("description",resource.getDescription());
        queryWrapper.eq("category_id",resource.getCategoryId());
        if (resourceService.count(queryWrapper) > 0) {
            return new ResponseResult(205,"注册失败，用户名已存在");
        }

        resource.setCreateTime(LocalDateTime.now());
        //在数据库中保存管理员对象
        resourceService.save(resource);
        return new ResponseResult(200,"注册成功");
    }

    @ApiOperation("修改资源信息")
    @PostMapping("/update/{id}")
    public ResponseResult update(@RequestBody Resource resource,
                                 @PathVariable("id") long id
                               ) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        resourceService.update(resource,updateWrapper);
        return new ResponseResult(200,"修改成功");
    }


}
